package dao

import (
	"ruoyi-hello/app/sys/model"
	"ruoyi-hello/config"
	"slices"
)

var TableSysRoleTable = "sys_role"
var TableUserSysRoleTable = "sys_user_role"

func SelectRolePermissionByUserId(userId int) []model.SysRole {

	var userRoles []model.SysUserRole
	err := config.MysqlConn.Primary.Table(TableUserSysRoleTable).Where(" user_id = ?", userId).Find(&userRoles)
	if err.Error != nil || len(userRoles) <= 0 {
		return nil
	}
	roleIds := make([]int, len(userRoles))
	for _, role := range userRoles {
		if !slices.Contains(roleIds, role.RoleId) {
			roleIds = append(roleIds, role.RoleId)
		}
	}
	var sysRoles []model.SysRole
	err = config.MysqlConn.Primary.Table(TableSysRoleTable).Where(" role_id in ?", roleIds).Find(&sysRoles)
	if err.Error != nil || len(sysRoles) <= 0 {
		return nil
	}
	return sysRoles
}
